package com.glory.wxmemsicpda.activity

import androidx.databinding.DataBindingUtil
import com.glory.wxmemsicpda.R
import com.glory.wxmemsicpda.base.App.Companion.app
import com.glory.wxmemsicpda.base.BaseDialogActivity
import com.glory.wxmemsicpda.bean.SheetInfoBean
import com.glory.wxmemsicpda.bean.net.BaseResponseBean
import com.glory.wxmemsicpda.databinding.ActivityCheckTableBinding
import com.glory.wxmemsicpda.databinding.ActivityDownloadRecipeBinding
import com.glory.wxmemsicpda.utils.NetUtils
import com.google.gson.JsonObject
import com.lzy.okgo.model.Response
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_SimpleDialogEntityCallBack
import com.pda.platform.ui.ui_pdaplatform.dialog.FreeUI_GeneralFragmentDialog
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DialogUtils
import kotlinx.android.synthetic.main.activity_download_recipe.*

/**
 * Recipe下载
 */
class DownLoadRecipeActivity : BaseDialogActivity() {
    private var mSheetInfoBean: SheetInfoBean = SheetInfoBean()
    private var viewDataBinding: ActivityDownloadRecipeBinding? = null
    private var tipDialog:FreeUI_GeneralFragmentDialog? = null

    override fun getLayoutResId(): Int {
        return R.layout.activity_download_recipe
    }

    override fun initView() {
        viewDataBinding = DataBindingUtil.setContentView(this, R.layout.activity_download_recipe)
        setTitle("Recipe下载")

        //初始化编辑框
        initEdit(etLot, object : editTextListener {
            override fun onTextChangeListener(s: String) {
                requestRecipeInfo(s)
            }
        })

    }

    override fun initData() {
        tvDownLoad.setOnClickListener{
            if (etEqpId.text.toString() == ""){
                showToast("设备编号不能为空",false)
                return@setOnClickListener
            }

            if (etLot.text.toString() == ""){
                showToast("批次编号不能为空",false)
                return@setOnClickListener
            }

            if (etLot.text.toString() == ""){
                showToast("程序名称不能为空",false)
                return@setOnClickListener
            }

            val map = JsonObject()
            map.addProperty("EQUIPMENTID", etEqpId.text.toString())//设备编号
            map.addProperty("LOTID", etLot.text.toString())//批次编号
            map.addProperty("RECIPENAME", mSheetInfoBean.lot.recipename)//程序名称
            NetUtils.requestPosNet(this,
                "MEMSICDOWNRECIPE", "MEMSICDOWNRECIPE",
                map,
                object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<SheetInfoBean?>?>(
                    this
                ) {
                    override fun onSuccess(response: Response<BaseResponseBean<SheetInfoBean?>?>) {
                        super.onSuccess(response)
                        if (response.body()!!.isSuccess(false, supportFragmentManager)) {
                            tipDialog = FreeApi_DialogUtils.getConfirmDialog(
                                app, "确定", "Recipe下载成功"
                            ) {
                                finish()
                            }
                            tipDialog!!.show(supportFragmentManager, "DIALOG")
                        }
                    }

                    override fun onError(response: Response<BaseResponseBean<SheetInfoBean?>?>) {
                        super.onError(response)
                        loadError(response.exception, "MEMSICDOWNRECIPE")
                    }
                })
        }
    }

    override fun getBaseEntity(): FreeUI_AddViewEntity {
        return FreeUI_AddViewEntity("header", llMain)
    }

    //查询Recipe信息
    private fun requestRecipeInfo(s: String) {

        if (etEqpId.text.toString() == ""){
            showToast("设备编号不能为空",false)
            return
        }

        val map = JsonObject()
        map.addProperty("EQUIPMENTID", etEqpId.text.toString())//设备编号
        map.addProperty("LOTID", s)//批次编号
        NetUtils.requestPosNet(this,
            "MEMSICLOTQUERY", "MEMSICLOTQUERY",
            map,
            object : FreeUI_SimpleDialogEntityCallBack<BaseResponseBean<SheetInfoBean?>?>(
                this
            ) {
                override fun onSuccess(response: Response<BaseResponseBean<SheetInfoBean?>?>) {
                    super.onSuccess(response)
                    if (response.body()!!.isSuccess(true, supportFragmentManager)) {
                        mSheetInfoBean = response.body()!!.response.body!!
                        viewDataBinding?.sheetInfoBean = mSheetInfoBean
                    }
                }

                override fun onError(response: Response<BaseResponseBean<SheetInfoBean?>?>) {
                    super.onError(response)
                    loadError(response.exception, "MEMSICLOTQUERY")
                }
            })
    }

}